<?php

namespace addons\booking\controller;

use addons\booking\model\RoomState;
use addons\booking\model\Store as StoreModel;
use addons\booking\model\Worker as WorkerModel;
use addons\booking\model\HouseMode;
use addons\booking\model\HouseFloor;
use addons\booking\model\House as HouseModel;
use addons\booking\model\HouseGuide;
use addons\booking\model\StoreOrder;
use think\Db;
use think\Session;


class Room extends Base
{
    protected $noNeedLogin = ['cleaning_guidance','get_mark_price'];
    protected $modelValidate = true; //是否开启Validate验证，默认是false关闭状态
    protected $modelSceneValidate = true; //是否开启模型场景验证，默认是false关闭状态

    /**
     * 获取房态
     * @param $store_id 酒店id
     * @param $sort_type 排序类型  楼层:house_floor  房型:house_mode  状态 house_status
     * @param $date_type 时间类型  1:今日  2:明日
     * @return $data
     */
    public function room_state()
    {
        $store_id = $this->request->param('store_id/d',0);
        $sort_type = $this->request->param('sort_type','house_floor');
        $date = $this->request->param('date/d','20230606');
        #获取所有的房间
        $houseAll = HouseModel::get_all_house($store_id,$date);

        $houseList = [];
        if($sort_type == 'house_floor'){
            //获取楼栋楼层
            $houseList =  HouseFloor::get_room_floor($store_id);

            foreach ($houseList as $k=>$v){
                foreach ($houseAll as $key=>$value){
                    if($value['floor_config_id'] == $v['id']){
                        $houseList[$k]['house_list'][] = $value;
                    }
                }
            }
        } else if($sort_type == 'house_mode'){
            //获取房型
            $houseList =  HouseMode::get_room_model($store_id);
            foreach ($houseList as $k=>$v){
                foreach ($houseAll as $key=>$value){
                    if($value['type_config_id'] == $v['id']){
                        $houseList[$k]['house_list'][] = $value;
                    }
                }
            }
        } else if($sort_type == 'house_status'){
            //组装房态
            $houseList = [
                ['id'=>'','name'=>'净房','status'=>0,'house_list'=>[]],
                ['id'=>'','name'=>'脏房','status'=>1,'house_list'=>[]],
                ['id'=>'','name'=>'已分派','status'=>2,'house_list'=>[]],
                ['id'=>'','name'=>'打扫中','status'=>3,'house_list'=>[]],
                ['id'=>'','name'=>'待验收','status'=>4,'house_list'=>[]],
            ];


                foreach ($houseAll as $key=>$value){
                    if(empty($value['room_state'])){
                        $houseList[0]['house_list'][] = $value;
                    } else {
                        $houseList[$value['room_state']['status']]['house_list'][] = $value;
                    }

                }

        }
        foreach ($houseList as $key=>$value){
            foreach ($value['house_list'] as $k=>$v) {
                $now_order = StoreOrder::alias('s')
                    ->join('store_order_son n','s.id=n.order_id','left')
                    ->join('order_room r','s.id=r.order_id','left')
                    ->join('worker w','w.id=r.worker_id','left')
                    ->where(['s.store_id'=>$v['store_id'],'s.status'=>['in',['0','1']],'r.room_id'=>$v['id']])
                    ->field('s.id,s.order_sn,w.user_name,s.create_time,n.arrival_time,n.service_start_time,n.service_end_time')
                    ->order('s.create_time desc')
                    ->find();
                $last_order =  StoreOrder::alias('s')
                    ->join('order_room r','s.id=r.order_id','left')
                    ->join('worker w','w.id=r.worker_id','left')
                    ->where(['s.store_id'=>$v['store_id'],'s.status'=>['in',['2','3']],'r.room_id'=>$v['id'],'r.status'=>4])
                    ->field('s.id,s.order_sn,w.user_name,s.arrival_time')
                    ->order('s.create_time desc')
                    ->find();
                $houseList[$key]['house_list'][$k]['now_order'] = $now_order;
                $houseList[$key]['house_list'][$k]['last_order'] = $last_order;
            }

        }
        $this->success('成功',$houseList);
    }

    //置净
    public function house_clean()
    {
        $store_id = $this->request->param('store_id','');
        $house_ids = $this->request->param('house_ids','');
        $date = $this->request->param('date','');
        if(!$store_id) $this->error('酒店id不能为空');
        if(!$house_ids) $this->error('请选择房间');
        if(!$date) $this->error('请选择时间');

        $house_ids_arr = explode(',',$house_ids);
        RoomState::check($store_id,$house_ids_arr,$date);

        $map['status'] = 0;
        $map['mark_price'] = 0;
        $where['date'] = $date;
        $where['house_id'] = ['in',$house_ids_arr];
        $res =  RoomState::where($where)->update($map);
        if($res){
            $this->success('操作成功');
        } else{
            $this->error('操作失败');
        }

    }

    //置脏操作
    public function house_dirtying()
    {
        $store_id = $this->request->param('store_id','');
        $house_ids = $this->request->param('house_ids','');
        $date = $this->request->param('date','');
        if(!$store_id) $this->error('酒店id不能为空');
        if(!$house_ids) $this->error('请选择房间');
        if(!$date) $this->error('请选择时间');

        $house_ids_arr = explode(',',$house_ids);
        RoomState::check($store_id,$house_ids_arr,$date);

        $map['status'] = 1;

        $where['date'] = $date;
        $where['house_id'] = ['in',$house_ids_arr];
        $res =  RoomState::where($where)->update($map);
        if($res){
            $this->success('操作成功');
        } else{
            $this->error('操作失败');
        }
    }

    //锁定操作
    public function house_lock()
    {
        $store_id = $this->request->param('store_id','');
        $house_ids = $this->request->param('house_ids','');
        $date = $this->request->param('date','');
        $lock_reason = $this->request->param('lock_reason','');
        if(!$store_id) $this->error('酒店id不能为空');
        if(!$house_ids) $this->error('请选择房间');
        if(!$date) $this->error('请选择时间');
        if(!$lock_reason) $this->error('锁定原因必须填写');

        $house_ids_arr = explode(',',$house_ids);

        RoomState::check($store_id,$house_ids_arr,$date);
        $map['is_lock'] = 1;
        $map['lock_reason'] = $lock_reason;
        $map['lock_time'] = time();

        $where['date'] = $date;
        $where['house_id'] = ['in',$house_ids_arr];
        $res =  RoomState::where($where)->update($map);
        if($res){
            $this->success('操作成功');
        } else{
            $this->error('操作失败');
        }

    }

    //房间解锁
    public function house_unlock()
    {
        $store_id = $this->request->param('store_id','');
        $house_ids = $this->request->param('house_ids','');
        $date = $this->request->param('date','');
        if(!$store_id) $this->error('酒店id不能为空');
        if(!$house_ids) $this->error('请选择房间');
        if(!$date) $this->error('请选择时间');

        $house_ids_arr = explode(',',$house_ids);
        $where['date'] = $date;
        $where['house_id'] = ['in',$house_ids_arr];
        $res =  RoomState::where($where)->update(['is_lock'=>0,'unlock_time'=>time()]);
        if($res){
            $this->success('操作成功');
        } else{
            $this->error('操作失败');
        }
    }

    //获取加价金额配置
    public function get_mark_price(){
        $data = [
            ['id'=>0,'mark_price'=>5],
            ['id'=>0,'mark_price'=>10],
            ['id'=>0,'mark_price'=>15],
            ['id'=>0,'mark_price'=>20],
        ];
        $this->success('',$data);
    }

    //重脏加价操作
    public function house_mark_price()
    {
        $id = $this->request->param('id',0);
        if(!$id)  $this->error('参数错误');
        $mark_price = $this->request->param('mark_price',0);
        if(!$mark_price) $this->error('请选择加价金额');

        $map['mark_price'] = $mark_price;
        $res =  RoomState::where('id',$id)->update($map);
        $this->success('操作成功');
    }

    //指派操作

    //验收操作

    //清洁指导
    public function cleaning_guidance()
    {
        $house_id = $this->request->param('house_id','');
        if(!$house_id) $this->error('参数错误');

        //房间
        $room = HouseModel::where('id',$house_id)->find();
        //清洁指导
        $house_guide = HouseGuide::where('id',$room['guide_id'])->field('cleaning_content,put_content')->find();

        $data['cleaning_content'] = $house_guide['cleaning_content'] ?: '';
        $data['put_content'] = $house_guide['put_content'] ?: '';
        $this->success('成功',$data);
    }
}
